package com.example.base.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.base.R;
import com.example.base.activity.LoginActivity;
import com.example.base.activity.SearchActivity;
import com.example.base.bean.QuestionThree;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class SerachViewAdapter extends BaseAdapter {
    public List<QuestionThree> getDataList() {
        return dataList;
    }
    private List<QuestionThree> dataList=new ArrayList<>();
    private Context context;

    public SerachViewAdapter(SearchActivity serachActivity, List<QuestionThree> questions) {
        this.dataList.addAll(questions);
        this.context = serachActivity;
    }


    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView==null){
            myViewHolder=new MyViewHolder();
            LayoutInflater inflater=LayoutInflater.from(context);
            convertView=inflater.inflate(R.layout.list_view_question,null,true);
            myViewHolder.tv_from=convertView.findViewById(R.id.tv_qus_from);
            myViewHolder.tv_time=convertView.findViewById(R.id.tv_qus_time);
            myViewHolder.tv_title=convertView.findViewById(R.id.tv_qus_title);
            myViewHolder.tv_name=convertView.findViewById(R.id.tv_qus_name);
            myViewHolder.tv_text=convertView.findViewById(R.id.tv_qus_text);
            myViewHolder.imageView=convertView.findViewById(R.id.img_ques);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder= (MyViewHolder) convertView.getTag();
        }
        QuestionThree questionThree=dataList.get(position);
        myViewHolder.tv_from.setText(questionThree.getAuthor());
        myViewHolder.tv_time.setText(questionThree.getNiceDate());
        myViewHolder.tv_title.setText(Html.fromHtml(questionThree.getTitle()));
        myViewHolder.tv_name.setText(questionThree.getChapterName());
        myViewHolder.tv_text.setText(Html.fromHtml(questionThree.getDesc()));
        SharedPreferences sp=convertView.getContext().getSharedPreferences("cookies", Context.MODE_PRIVATE);
        String loginusername=sp.getString("loginUserName_wanandroid_com","");
        myViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (loginusername!=null&&loginusername.length()!=0){
                    String urileft="https://www.wanandroid.com/lg/collect/";
                    int id;
                    String uriRight="/json";
                    id=questionThree.getId();
                    SharedPreferences sp=v.getContext().getSharedPreferences("cookies", Context.MODE_PRIVATE);
                    String value1=sp.getString("loginUserName_wanandroid_com","null");
                    String value2=sp.getString("loginUserName","null");
                    String value3=sp.getString("token_pass_wanandroid_com","null");
                    String value4=sp.getString("token_pass","null");
                    OkHttpClient.Builder client=new OkHttpClient.Builder();
                    RequestBody body = new FormBody.Builder()
                            .add("id", String.valueOf(id))
                            .build();
                    Request request = new Request.Builder()
                            .url(urileft+id+uriRight)
                            .post(body)
                            .addHeader("cookie",value1)
                            .addHeader("cookie",value2)
                            .addHeader("cookie",value3)
                            .addHeader("cookie",value4)
                            .build();
                    Call call = client.build().newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {

                        }

                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                        }
                    });
                    Toast.makeText(context,"收藏成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(context,"请先登录",Toast.LENGTH_LONG).show();
                    Intent intent=new Intent(context, LoginActivity.class);
                    context.startActivity(intent);
                }
            }
        });
        return convertView;
    }


    public void setData(List<QuestionThree> data) {
        dataList.clear();
        dataList.addAll(data);
    }


    class MyViewHolder {
        TextView tv_from;
        TextView tv_time;
        TextView tv_title;
        TextView tv_name;
        TextView tv_text;
        ImageView imageView;
    }
}
